/**
 * @Title: stationController.java
 * @Package tt.dz.mvc.controller.sys
 * @author pengyu
 * @date 2015年12月17日 上午10:21:55
 * @version V1.0
 */
package tt.dz.mvc.controller.sys;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tt.dz.dao.ext.charger.OmsChargerDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.charger.ErrorCharger;
import tt.dz.entity.charger.OmsCharger;
import tt.dz.entity.charger.OmsChargerErrorRecord;
import tt.dz.entity.charger.OmsChargerRate;
import tt.dz.service.charger.OmsChargerErrorRecordService;
import tt.dz.util.ActionUtils;
import tt.dz.util.OmsConstant;
import tt.dz.util.StringUtil;

@Controller
@RequestMapping("chargerErrorRecordApi")
public class ChargerErrorRecordController {

	@Resource
	OmsChargerErrorRecordService omsChargerErrorRecordService;
	@Resource
	OmsChargerDao omsChargerDao;

	@SuppressWarnings("rawtypes")
	@RequestMapping("/getList")
	@ResponseBody
	public JsonForList getList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JsonForList isLogin = StringUtil.isLogin(request);
		if (isLogin.getCode().equals("400")) {
			return isLogin;
		}
		// HttpSession session = request.getSession();
		// String login_oper_id = (String)
		// session.getAttribute("login_oper_id");
		OmsChargerErrorRecord record = new OmsChargerErrorRecord();
		String charger_error_id = request.getParameter("charger_error_id");
		if (StringUtil.isNumeric(charger_error_id)) {
			record.setCharger_error_id(Long.valueOf(charger_error_id));
		}
		String charger_id = request.getParameter("charger_id");
		if (StringUtil.isNotBlank(charger_id)) {
			record.setCharger_id(charger_id);
		}
		String error_code = request.getParameter("error_code");
		if (StringUtil.isNotBlank(error_code)) {
			record.setError_code(error_code);
		}
		String status = request.getParameter("status");
		if (StringUtil.isNumeric(status)) {
			record.setStatus(Integer.valueOf(status));
		}
		String valid_flag = request.getParameter("valid_flag");
		if (StringUtils.isNumeric(valid_flag)) {
			record.setValid_flag(Integer.valueOf(valid_flag));
		}
		return omsChargerErrorRecordService.findList(record);
	}

	@SuppressWarnings("rawtypes")
	@RequestMapping("/getPageList")
	@ResponseBody
	public JsonForList getPageList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 搜索条件：可选
		String searchStr = request.getParameter("searchStr");
		if (StringUtil.isNotBlank(searchStr)) {
			searchStr = new String(searchStr.getBytes("iso-8859-1"), "utf-8");
		}

		return omsChargerErrorRecordService.findPageList(ActionUtils.buildQueryParam(request, true), searchStr);
	}

	@SuppressWarnings("rawtypes")
	@RequestMapping("/update")
	@ResponseBody
	public JsonCommon update(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		String login_oper_id = (String) session.getAttribute("login_oper_id");
		OmsChargerErrorRecord query = new OmsChargerErrorRecord();
		String charger_error_id = request.getParameter("charger_error_id");
		if (StringUtil.isNumeric(charger_error_id)) {
			query.setCharger_error_id(Long.valueOf(charger_error_id));
		} else {
			return new JsonCommon(null, OmsConstant.ErrorCodeEnum.MessageMissParam.value(),
					OmsConstant.ErrorCodeEnum.MessageMissParam.desc());
		}
		OmsChargerErrorRecord record = omsChargerErrorRecordService.findOneExample(query, null, null);
		if (record == null) {
			return new JsonCommon(null, OmsConstant.ErrorCodeEnum.ParamTypeError.value(),
					OmsConstant.ErrorCodeEnum.ParamTypeError.desc());
		}
		String charger_id = request.getParameter("charger_id");
		if (StringUtil.isNotBlank(charger_id)) {
			record.setCharger_id(charger_id);
		}
		String error_code = request.getParameter("error_code");
		if (StringUtil.isNotBlank(error_code)) {
			record.setError_code(error_code);
		}
		String status = request.getParameter("status");
		if (StringUtil.isNumeric(status)) {
			record.setStatus(Integer.valueOf(status));
		}
		String handle_text = request.getParameter("handle_text");
		if (StringUtil.isNotBlank(handle_text)) {
			record.setHandle_text(handle_text);
		}
		String handle_time = request.getParameter("handle_time");// 年月日时分秒
		if (StringUtil.isNotBlank(handle_time)) {
			record.setHandle_time(StringUtil.StrToDate(handle_time));
		}
		String valid_flag = request.getParameter("valid_flag");
		if (StringUtils.isNumeric(valid_flag)) {
			record.setValid_flag(Integer.valueOf(valid_flag));
		}
		record.setUpdate_oper_id(login_oper_id);
		record.setUpdate_time(new Date());
		return omsChargerErrorRecordService.update(record);
	}

	@SuppressWarnings("rawtypes")
	@RequestMapping("/getErrorRecordByChargerId")
	@ResponseBody
	public JsonForList getErrorRecordByChargerId(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String charger_id = request.getParameter("charger_id");
		// 开始需要查询电桩为故障状态的最新一条故障信息：现修正为该电桩的最近一条故障信息，不管该电桩是否故障
		// String sql = "";
		// Integer errorTotal = 0 ;
		//
		// String condition = " where charger_id = '" + charger_id + "'";
		// OmsCharger omsCharger = omsChargerDao.findOneExampleByCondition(new
		// OmsCharger(), condition);
		// switch(omsCharger.getCharger_type()){//设备电流类型:1--直流 2--交流
		// case 1:
		// //先查询直流桩状态表
		// sql = "select count(*) as total from oms_charger_state_direct where
		// charge_status = 3 and valid_flag = 1 ";
		// List<Map<String, Object>> listDirect =
		// omsChargerDao.findDataBySql(sql, null);
		// Map<String, Object> mapDirect = listDirect.get(0);
		// errorTotal = Integer.valueOf(mapDirect.get("total").toString());
		//
		// case 2:
		// //先查询直流桩状态表
		// sql = "select count(*) as total from oms_charger_state_alternate
		// where charge_status = 3 and valid_flag = 1 ";
		// List<Map<String, Object>> listAlternate =
		// omsChargerDao.findDataBySql(sql, null);
		// Map<String, Object> mapAlternate = listAlternate.get(0);
		// errorTotal = Integer.valueOf(mapAlternate.get("total").toString());
		//
		// }

		// List<OmsChargerErrorRecord> list = new
		// ArrayList<OmsChargerErrorRecord>();
		// if ( errorTotal>0 ) {
		// OmsChargerErrorRecord omsChargerErrorRecord =new
		// OmsChargerErrorRecord();
		// omsChargerErrorRecord.setCharger_id(charger_id);
		// omsChargerErrorRecord =
		// omsChargerErrorRecordService.findOneExample(omsChargerErrorRecord,"create_time","desc");
		// list.add(omsChargerErrorRecord);
		// }

		// JsonForList json = new JsonForList();
		// json.setList(list);
		// json.setCode(JsonForList.SuccessCode);

		String errormsg = null;

		if (StringUtil.isBlank(charger_id)) {
			errormsg = "ChargerID should not be all empty";
		}

		JsonForList json = new JsonForList();
		if (errormsg != null) {
			json.setCode(JsonForList.FailCode);
			json.setMsg(errormsg);
		} else {
			List<OmsChargerErrorRecord> list = new ArrayList<OmsChargerErrorRecord>();
			// OmsChargerErrorRecord omsChargerErrorRecord =new
			// OmsChargerErrorRecord();
			// omsChargerErrorRecord.setCharger_id(charger_id);
			// omsChargerErrorRecord =
			// omsChargerErrorRecordService.findOneExample(omsChargerErrorRecord,"create_time","desc");
			// list.add(omsChargerErrorRecord);

			list = omsChargerErrorRecordService.findListByCondition(charger_id);

			json.setList(list);
			json.setCode(JsonForList.SuccessCode);
		}

		return json;
	}

	// 查询历史的故障电桩数量，交流 直流数量 distinct
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/getErrorRecordCount")
	@ResponseBody
	public JsonCommon getErrorRecordCount(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// oms_charger_error_record
		// 开始需要查询电桩为故障状态的最新一条故障信息：现修正为该电桩的最近一条故障信息，不管该电桩是否故障
		
		Integer errorAtotal = 0;// 交流数量
		Integer errorDtotal = 0;// 交流数量

		// 先查询直流桩
		String sqlD = "select * from oms_charger where valid_flag = 1 and charger_type = 1 and charger_id in (select charger_id from oms_charger_error_record) ";
//	    List<OmsCharger> dChargerList = omsChargerDao.findExample(sqlD, null, null, null, null);
	    List<OmsCharger> dChargerList =  omsChargerDao.findDataBySqlOfClass(sqlD, OmsCharger.class, null);
	    if(dChargerList!= null && dChargerList.size()>0){
	    	errorDtotal  = dChargerList.size();
	    }
		// 先查询交流桩
	    String sqlA = "select * from oms_charger where valid_flag = 1 and charger_type = 2 and charger_id in (select charger_id from oms_charger_error_record) ";
//	    List<OmsCharger> aChargerList = omsChargerDao.findExample(sqlA, null, null, null, null);
	    List<OmsCharger> aChargerList =  omsChargerDao.findDataBySqlOfClass(sqlA, OmsCharger.class, null);
	    if(aChargerList!= null && aChargerList.size()>0){
	    	errorAtotal  = aChargerList.size();
	    }
	    ErrorCharger errorCharger = new ErrorCharger();
	    errorCharger.setaChargerList(aChargerList);
	    errorCharger.setdChargerList(dChargerList);
	    errorCharger.setErrorAtotal(errorAtotal);
	    errorCharger.setErrorDtotal(errorDtotal);
	    
		BigDecimal total = new BigDecimal(errorAtotal).add(new BigDecimal(errorDtotal));
		BigDecimal aPercent = new BigDecimal(0);
		BigDecimal dPercent = new BigDecimal(0);
		
		if(errorAtotal>0 && errorDtotal <= 0){
			aPercent = new BigDecimal(1);//100%
		}else if(errorAtotal <=0 && errorDtotal > 0){
			dPercent = new BigDecimal(1);//100%
		}else if(total.intValue() != 0){
			aPercent = new BigDecimal(errorAtotal).divide(total, 2, BigDecimal.ROUND_HALF_UP);
			dPercent = new BigDecimal(errorDtotal).divide(total, 2, BigDecimal.ROUND_HALF_UP);
		}
	    
		errorCharger.setaPercent(aPercent);
		errorCharger.setdPercent(dPercent);
	    
		JsonCommon json = new JsonCommon();
		json.setPojo(errorCharger);
		json.setCode(JsonForList.SuccessCode);
		return json;
	}
	
	public static void main(String[] args){
//		
//		Integer errorAtotal = 9;// 交流数量
//		Integer errorDtotal = 19;// 交流数量
//		
//		BigDecimal total = new BigDecimal(errorAtotal).add(new BigDecimal(errorDtotal));
//		BigDecimal aPercent = new BigDecimal(0);
//		BigDecimal dPercent = new BigDecimal(0);
//		
//		
//		if(errorAtotal>0 && errorDtotal <= 0){
//			aPercent = new BigDecimal(1);//100%
//		}else if(errorAtotal <=0 && errorDtotal > 0){
//			dPercent = new BigDecimal(1);//100%
//		}else if(total.intValue() != 0){
//			aPercent = total.divide(new BigDecimal(errorAtotal), 2, BigDecimal.ROUND_HALF_UP);
//			dPercent = total.divide(new BigDecimal(errorDtotal), 2, BigDecimal.ROUND_HALF_UP);
//		}
//	    
//		 
//		System.out.println(aPercent);
//		System.out.println(dPercent);
		
		BigDecimal total = new BigDecimal("100");
		BigDecimal aPercent = new BigDecimal("22.3");
		BigDecimal dPercent = new BigDecimal("44.58");
		BigDecimal a = aPercent.divide(total, 2, BigDecimal.ROUND_HALF_DOWN);
		BigDecimal d = total.divide(dPercent, 2, BigDecimal.ROUND_HALF_UP);
		System.out.println(a);
		System.out.println(d);
		
	}

}
